#include<iostream>
#include<pthread.h>
#include<vector>
#include<string.h>
#include<unistd.h>
pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
#define NUM 5
int cnt=1000;
void* thread_func(void*arg){
    std::string name=(char*)arg;
    while(true){
        pthread_mutex_lock(&lock);
        //直接让线程进行等待
        pthread_cond_wait(&cond,&lock);
        //线程被唤醒后，进行操作
        std::cout<<" wake up"<<std::endl;
        std::cout<<name<<" cnt "<<cnt<<std::endl;
        cnt--;
        pthread_mutex_unlock(&lock);
    }
}
int main(){
    std::vector<pthread_t> threads;
    for(int i=0;i<5;i++){
        pthread_t tid;
        std::string name="thread"+std::to_string(i);
       int ret=pthread_create(&tid,NULL,thread_func,(void*)name.c_str());
       if(ret!=0){
           continue;
       }
       threads.push_back(tid);
       sleep(1);
    }
    while(true){
        //pthread_cond_signal(&cond);
        pthread_cond_broadcast(&cond);
        sleep(1);
    }
    for(auto&tid:threads){
        int m=pthread_join(tid,NULL);
    }
    return 0;
}